package com.dhcc.bpm.modules.system.controller;

import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.system.entity.Dept;
import com.dhcc.bpm.modules.system.entity.Role;
import com.dhcc.bpm.modules.system.entity.User;
import com.dhcc.bpm.modules.system.service.IDeptService;
import com.dhcc.bpm.modules.system.service.IRoleService;
import com.dhcc.bpm.modules.system.service.IUserService;
import com.dhcc.bpm.modules.system.vo.DeptVO;
import com.dhcc.bpm.modules.system.vo.RoleVO;
import com.dhcc.bpm.modules.system.wrapper.DeptWrapper;
import com.dhcc.bpm.modules.system.wrapper.RoleWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.constant.AuthConstant;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.node.INode;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
/**
 * @ClassName DeptController
 * @Description 控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/common")
@Api(value = "公共接口", tags = "公共接口")
public class CommonController extends BladeController {

	private IDeptService deptService;

	private IUserService userService;

	private IRoleService roleService;

	/**
	 * @Description 列表
	 * @Author sjm
	 * @Date 10:02 2020/2/22 0022
	 * @param dept
	 * @return org.springblade.core.tool.api.R<java.util.List<org.springblade.core.tool.node.INode>>
	 **/
	@GetMapping("/deptList")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "deptName", value = "部门名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "fullName", value = "部门全称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表", notes = "传入dept")
    @ApiLog("部门列表")
	@Log(title = "部门列表", businessType = BusinessType.SELECT)
	public R<List<INode>> deptList(@ApiIgnore @RequestParam Map<String, Object> dept) {
		List<Dept> list = deptService.list(Condition.getQueryWrapper(dept, Dept.class).lambda().orderByAsc(Dept::getSort));
		return R.data(DeptWrapper.build().listNodeVO(list));
	}

	/**
	 * @Description 获取部门树形结构
	 * @Author sjm
	 * @Date 10:02 2020/2/22 0022
	 * @param tenantId
	 * @param bladeUser
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.vo.DeptVO>>
	 **/
	@GetMapping("/deptTree")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "树形结构", notes = "树形结构")
    @ApiLog("获取部门树形结构")
	@Log(title = "获取部门树形结构", businessType = BusinessType.SELECT)
	public R<List<DeptVO>> deptTree(String tenantId, BladeUser bladeUser) {
		List<DeptVO> tree = deptService.tree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()));
		return R.data(tree);
	}

	/**
	 * @Description 用户列表
	 * @Author sjm
	 * @Date 10:10 2020/2/22 0022
	 * @param user
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.entity.User>>
	 **/
	@GetMapping("/user-list")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "用户列表", notes = "传入user")
	@ApiLog("用户列表")
	@Log(title = "用户列表", businessType = BusinessType.SELECT)
	public R<List<User>> userList(User user) {
		List<User> list = userService.list(Condition.getQueryWrapper(user));
		return R.data(list);
	}


	/**
	 * @Description 列表
	 * @Author sjm
	 * @Date 10:06 2020/2/22 0022
	 * @param role
	 * @return org.springblade.core.tool.api.R<java.util.List<org.springblade.core.tool.node.INode>>
	 **/
	@GetMapping("/list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roleName", value = "参数名称", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "roleAlias", value = "角色别名", paramType = "query", dataType = "string")
	})
	@PreAuth(AuthConstant.HAS_ROLE_ADMIN)
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表", notes = "传入role")
	@ApiLog("查询角色列表")
	@Log(title = "查询角色列表", businessType = BusinessType.SELECT)
	public R<List<INode>> list(@ApiIgnore @RequestParam Map<String, Object> role) {
		List<Role> list = roleService.list(Condition.getQueryWrapper(role, Role.class).lambda().orderByAsc(Role::getSort));
		return R.data(RoleWrapper.build().listNodeVO(list));
	}

	/**
	 * @Description 获取角色树形结构
	 * @Author sjm
	 * @Date 10:06 2020/2/22 0022
	 * @param tenantId
	 * @param bladeUser
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.vo.RoleVO>>
	 **/
	@GetMapping("/tree")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	@ApiLog("获取角色树形结构")
	@Log(title = "获取角色树形结构", businessType = BusinessType.SELECT)
	public R<List<RoleVO>> tree(String tenantId, BladeUser bladeUser) {
		List<RoleVO> tree = roleService.tree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()));
		return R.data(tree);
	}
}
